<?php
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Group
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigPageMain');

class BxGroupsPageMain extends BxDolTwigPageMain {

    function BxGroupsPageMain(&$oMain) {
        $this->sSearchResultClassName = 'BxGroupsSearchResult';
        $this->sFilterName = 'bx_groups_filter';
		parent::BxDolTwigPageMain('bx_groups_main', $oMain);
	}
 
    function getBlockCode_Map() {
 
		$sMeasurementType = getParam('bx_groups_measurement_type');
		$sBaseUri = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri();

		$iProfileId = getLoggedId();
        $aViewer = getProfileInfo($iProfileId);

		if($iProfileId){
 			$this->oTemplate->addInjection('injection_body', 'text',  'onload="load();searchLocations(\''.$sBaseUri.'\');"');
		}else{
 			$this->oTemplate->addInjection('injection_body', 'text',  'onload="load();"');
		}

		$this->oTemplate->addJs ('http://maps.google.com/maps/api/js?sensor=false');
 		$this->oTemplate->addJs ('gmap_multiple.js');

		switch($sMeasurementType){
			case 'miles':
				$sMeasurementTypeC = _t('_bx_groups_miles');
			break;
			case 'kilometers':
				$sMeasurementTypeC = _t('_bx_groups_kilometers');
			break;
			default:
				$sMeasurementTypeC = _t('_bx_groups_kilometers');
			break;
		}
 
		$aVars = array (  
			'city_country' => (trim($aViewer['City'])) ? trim($aViewer['City']) .','. $aViewer['Country'] : '',
			'zoom' => getParam('bx_groups_map_zoom'),
			'lat' => getParam('bx_groups_map_lat'),
			'long' => getParam('bx_groups_map_long'),
			'distance' => (int)getParam('bx_groups_map_default_distance'), 
			'distance_type' => $sMeasurementTypeC,
			'base_uri' => $sBaseUri 
		);
 
        return $this->oTemplate->parseHtmlByName('block_map_multiple', $aVars);
	} 
  
    function getBlockCode_LatestFeaturedGroup() {
        
        $aDataEntry = $this->oDb->getLatestFeaturedItem (); 
        if (!$aDataEntry) {
            return MsgBox(_t('_Empty'));
        }

        $aAuthor = getProfileInfo($aDataEntry['author_id']);

        $sImageUrl = ''; 
        $sImageTitle = ''; 
        $a = array ('ID' => $aDataEntry['author_id'], 'Avatar' => $aDataEntry['thumb']);
        $aImage = BxDolService::call('photos', 'get_image', array($a, 'file'), 'Search');

        bx_groups_import('Voting');
        $oRating = new BxGroupsVoting ('bx_groups', $aDataEntry['id']);

        $aVars = array (
            'image_url' => !$aImage['no_image'] && $aImage['file'] ? $aImage['file'] : $this->oTemplate->getIconUrl('no-photo-110.png'),
            'image_title' => !$aImage['no_image'] && $aImage['title'] ? $aImage['title'] : '',            
            'group_url' => BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() . 'view/' . $aDataEntry['uri'],
            'group_title' => $aDataEntry['title'],
            'author_title' => _t('_From'),
            'author_username' => $aAuthor['NickName'],
            'author_url' => getProfileLink($aAuthor['ID']),
            'rating' => $oRating->isEnabled() ? $oRating->getJustVotingElement (true, $aDataEntry['id']) : '',
        );
        return $this->oTemplate->parseHtmlByName('latest_featured_group', $aVars);
    }

    function getBlockCode_Recent() { 
        return $this->ajaxBrowse('recent', $this->oDb->getParam('bx_groups_perpage_main_recent'));
    }  
	

   //[begin] - ultimate groups mod from modzzz  
    function getBlockCode_Tags($iBlockId) { 
        bx_import('BxTemplTagsModule');
        $aParam = array(
            'type' => 'bx_groups',
            'orderby' => 'popular',
			'pagination' => getParam('tags_perpage_browse')
        );

		$sUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() . 'tags';
  
        $oTags = new BxTemplTags();
        $oTags->getTagObjectConfig();
    
        return array(
            $oTags->display($aParam, $iBlockId, '', $sUrl),
            array(),
            array(),
            _t('_Tags')
        ); 

    }  
    
    function getBlockCode_Categories() {
		bx_import('BxTemplCategories');
  		
		$sType = 'bx_groups';
		
		$oCateg = new BxTemplCategories();
		$oCateg->getTagObjectConfig();

	    $aAllEntries = $this->oDb->getCategories($sType);
    
        $aResult['bx_repeat:entries'] = array();        
 		foreach($aAllEntries as $aEntry)
		{	 
			$iNumCategory = $this->oDb->getCategoryCount($sType,$aEntry['Category']);	
	
			$sHrefTmpl = $oCateg->getHrefWithType($sType);  
			$sCategory = $aEntry['Category'];
            $sCatHref = str_replace( '{tag}', urlencode(title2uri($sCategory)), $sHrefTmpl);
 
	        $aResult['bx_repeat:entries'][] = array(
                'cat_url' => $sCatHref, 
                'cat_name' => $sCategory,
			    'num_items' => $iNumCategory, 
            );	        
	    } 
 
	    return $this->oTemplate->parseHtmlByName('group_categories', $aResult);  
	}
 
	function getBlockCode_Activities() {
		$iNumEntries = getParam("bx_groups_perpage_main_feed"); 
		$aActivity = $this->oDb->getActivityFeed($iNumEntries);
        
		if(empty($aActivity))
			return;

		$aResult['bx_repeat:entries'] = array();  
 		foreach($aActivity as $aEntry){
			 
			$iGroupId = $aEntry['group_id'];
			$sLangKey = _t($aEntry['lang_key']);
			$sParams = $aEntry['params'];
			$iActionDate = $aEntry['date'];

			$aDbParams = explode(";", $sParams);
			$aParams = array();
			foreach($aDbParams as $aEachParam) {
			
				$aParamItems = explode("|", $aEachParam);
				$sKey = $aParamItems[0];
				$sValue = $aParamItems[1];
				$aParams[$sKey] = $sValue;
			
				$sLangKey = str_replace('{'.$sKey.'}', $sValue, $sLangKey); 
			}
		  
			$aResult['bx_repeat:entries'][] = array(
			    'thumbnail' => $GLOBALS['oFunctions']->getMemberIcon($aParams['profile_id'], 'left'), 
 			    'description' => $sLangKey, 
 			    'date' => defineTimeInterval($iActionDate),  
			);	  
	    }

	    return $this->oTemplate->parseHtmlByName('group_activities', $aResult);  
	}

	function getBlockCode_Comments() 
	{   
		$iNumComments = getParam("bx_groups_perpage_main_comment");
		$aAllEntries = $this->oDb->getLatestComments($iNumComments);

		if(!count($aAllEntries))
			return; 
   
		$aVars = array (
			'bx_repeat:comments' => array (),
		);

		foreach($aAllEntries as $aEntry)
		{   
			$iMemberID = $aEntry['cmt_author_id'];
			$sNickName = getNickName($iMemberID);
			$sNickLink = getProfileLink($iMemberID);
			$icoMember = get_member_icon($iMemberID);
			$sMessage = $aEntry['cmt_text']; 
			$dtSent = defineTimeInterval($aEntry['date']);
			$iGroupId = $aEntry['cmt_object_id']; 
	 
			$iLimitChars = (int)getParam('bx_groups_comments_max_preview');
			if (strlen($sMessage) > $iLimitChars) {
				$sLinkMore = '...';
				$sMessage = process_line_output(mb_substr(strip_tags($sMessage), 0, $iLimitChars)) . $sLinkMore;
			}

			$aGroup = $this->oDb->getEntryById($iGroupId);
			$sGroupUri = $aGroup['uri'];
			$sGroupTitle = $aGroup['title'];
 
			$sGroupUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() .'view/' . $sGroupUri;
 

			$aVars['bx_repeat:comments'][] = array (
				'ico_member' => $icoMember, 
				'nicklink' => $sNickLink,
				'nickname' => $sNickName,
				'date' => $dtSent,
				'message' => $sMessage,
				'group_link' => $sGroupUrl,
				'group_title' => $sGroupTitle 
			);  
		}
 
		return $this->oTemplate->parseHtmlByName('block_comments', $aVars); 
	}
 
	function getBlockCode_Forum() {
  		
		$aPosts = $this->oDb->getAll("SELECT e.`title`, f.`forum_uri`, p.`user`, p.`post_text`, t.`topic_uri`, t.`topic_title`,p.`when` FROM `bx_groups_forum` f, `bx_groups_forum_topic` t, `bx_groups_forum_post` p, `bx_groups_main` e WHERE p.`topic_id`=t.`topic_id` AND t.`forum_id`=f.`forum_id` AND e.`ID`=f.`entry_id` AND e.`allow_post_in_forum_to`='3'  ORDER BY  p.`when`");
 		 
		$aVars['bx_repeat:entries'] = array();
  		foreach($aPosts as $aEachPost){

			$sForumUri = $aEachPost['forum_uri'];
			$sTopic = $aEachPost['topic_title']; 
			$sTopicUri = $aEachPost['topic_uri'];
			$sPostText = $aEachPost['post_text']; 
			$sDate = defineTimeInterval($aEachPost['when']); 
			$sGroupName = $aEachPost['title']; 
 			$sPoster = $aEachPost['user']; 

			$iLimitChars = (int)getParam('bx_groups_forum_max_preview');
			if (strlen($sPostText) > $iLimitChars) {
				$sLinkMore = '...';
				$sPostText = process_line_output(mb_substr(strip_tags($sPostText), 0, $iLimitChars)) . $sLinkMore;
			}


			$sGroupUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() .'view/' . $sForumUri;
			$sTopicUrl = BX_DOL_URL_ROOT . 'forum/groups/forum/'.$sForumUri.'-0.htm#topic/'.$sTopicUri.'.htm';
	
			$aVars['bx_repeat:entries'][] = array( 
							'topic_url' => $sTopicUrl, 
							'topic' => $sTopic, 
							'post' => $sPostText, 
							'group_name' => $sGroupName, 
							'group_url' => $sGroupUrl, 
							'post_date' => $sDate,
							'nicklink' => getProfileLink(getID($sPoster)),
							'nickname' => $sPoster,
 
						);
		}

		$sCode = $this->oTemplate->parseHtmlByName('group_forum', $aVars);  

		return $sCode;
	}

	function getBlockCode_Create() {
   		
		$sAskUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() . 'browse/my&bx_groups_filter=add_group'; 
    
		$aVars = array( 
			'create_url' => $sAskUrl, 
  		);
 
		$sCode = $this->oTemplate->parseHtmlByName('create_group', $aVars);  

		return $sCode;
	}

	function getBlockCode_Featured() { 
		return $this->ajaxBrowse('featured', 1);
	} 

	function getBlockCode_PopularList() { 
		return $this->ajaxBrowse('popular', $this->oDb->getParam('bx_groups_perpage_main_popular'));
	}     

	function getBlockCode_TopList() { 
		return $this->ajaxBrowse('top', $this->oDb->getParam('bx_groups_perpage_main_top'));
	}     

	function getBlockCode_Calendar() {  
	   $oMain = BxDolModule::getInstance('BxGroupsModule');
	   $iYear=(int)date("Y");
	   $iMonth=(int)date("m");
	   
		$sCode = "<div id='cal_block'>";
		$sCode .= $oMain->displayMainCalendar($iYear, $iMonth, _t('_bx_groups_calendar'));
		$sCode .= "</div>";

		return $sCode; 
	 }

	function getBlockCode_SearchOLD() {
		
		$sSearchUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() . 'browse/quick/'; 
	
		$aVars = array( 
			'search_url' => $sSearchUrl, 
		);
 
		$sCode = $this->oTemplate->parseHtmlByName('search_groups', $aVars);  

		return $sCode;
	} 

	function getBlockCode_Search() {
   
        $this->oTemplate->pageStart();
 
        bx_import('BxDolProfileFields');
        $oProfileFields = new BxDolProfileFields(0);
        $aCountries = $oProfileFields->convertValues4Input('#!Country');
        asort($aCountries);
        $aCountries = array_merge (array('' => _t('_bx_groups_all_countries')), $aCountries);
  
        $sStateUrl = BX_DOL_URL_ROOT . $this->oConfig->getBaseUri() . 'home/?ajax=state&country=' ;
       
        bx_import('BxDolCategories');
        $oCategories = new BxDolCategories();
        $oCategories->getTagObjectConfig ();
        $aCategories = $oCategories->getCategoriesList('bx_groups', (int)$iProfileId, true);
 
        $aCategories[''] = _t('_bx_groups_all_categories'); 
 
        $aForm = array(

            'form_attrs' => array(
                'name'     => 'form_search_groups',
                'action'   => '',
                'method'   => 'get',
            ),     

            'params' => array (
                'db' => array(
                    'submit_name' => 'submit_form',
                ),
            ),
                 
            'inputs' => array(
                'Keyword' => array(
                    'type' => 'text',
                    'name' => 'Keyword',
                    'caption' => _t('_bx_groups_form_caption_keyword'),
                    'required' => true,
                    'checker' => array (
                        'func' => 'length',
                        'params' => array(3,100),
                        'error' => _t ('_bx_groups_form_err_keyword'),
                    ),
                    'db' => array (
                        'pass' => 'Xss',
                    ),
                ),               
                'Category' => array(
                    'type' => 'select_box',
                    'name' => 'Category',
                    'caption' => _t('_bx_groups_form_caption_category'),
                    'values' => $aCategories,
                    'required' => false,
                    'db' => array (
                        'pass' => 'Xss',
                    ),                   
                ),
 
                'Country' => array(
                    'type' => 'select',
                    'name' => 'Country',
                    'caption' => _t('_bx_groups_form_caption_country'),
                    'values' => $aCountries,
                    'required' => false,
                    'attrs' => array(
                    'onchange' => "getHtmlData('substate','$sStateUrl'+this.value)",
                    ),
                    'db' => array (
                    'pass' => 'Preg',
                    'params' => array('/([a-zA-Z]{0,2})/'),
                    ),                   
                ),

                'State' => array(
                    'type' => 'select',
                    'name' => 'State',
                    'caption' => _t('_bx_groups_form_caption_state'),
                            'attrs' => array(
                    'id' => 'substate',
                            ),
                          'db' => array (
                    'pass' => 'Preg',
                    'params' => array('/([a-zA-Z]+)/'),
                    ),
                ),
                         
                'City' => array(
                    'type' => 'text',
                    'name' => 'City',
                    'caption' => _t('_bx_groups_form_caption_city'),
                    'required' => false,
                    'db' => array (
                    'pass' => 'Xss',
                    ),               
                ),
  
                'Submit' => array (
                    'type' => 'submit',
                    'name' => 'submit_form',
                    'value' => _t('_bx_groups_continue'),
                    'colspan' => true,
                ),
            ),           
        );

        $oForm = new BxTemplFormView($aForm);
        $oForm->initChecker(); 
 
        if ($oForm->isSubmittedAndValid ()) {
 
            bx_groups_import ('SearchResult');
            $o = new BxGroupsSearchResult('search', $oForm->getCleanValue('Keyword'), $oForm->getCleanValue('Category'), $oForm->getCleanValue('Country'), $oForm->getCleanValue('State'), $oForm->getCleanValue('City'));

            if ($o->isError) {
                $this->oTemplate->displayPageNotFound ();
                exit;
            }

            if ($s = $o->processing()) {
                echo $s;
            } else {
                $this->oTemplate->displayNoData ();
                exit;
            }

            $this->oMain->isAllowedSearch(true); // perform search action

            $this->oTemplate->addCss ('unit.css');
            $this->oTemplate->addCss ('main.css');
            $this->oTemplate->pageCode($o->aCurrent['title'], false, false);
            exit;

        }
 
        return $oForm->getCode(); 
    }  
     //[end] - ultimate groups mod from modzzz 

}

?>
